package com.hgyc.mom.tool.controller;

import java.util.*;

import com.hgyc.mom.tool.entity.CustomReportField;
import com.hgyc.mom.tool.service.CustomReportFieldService;
import com.hgyc.mom.tool.vo.CustomReportVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;
import com.hgyc.mom.tool.entity.CustomReport;
import com.hgyc.mom.tool.vo.CustomReportParamVO;
import com.hgyc.mom.tool.service.CustomReportService;
import com.hgyc.mom.common.vo.Result;

import com.hgyc.mom.core.mybatis.QueryWrapperUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

/**
 * 自定义报Controller
 *
 * @author fwj
 * @since 2025-04-29
 */
@Tag(name = "自定义报控制器", description = "自定义报控制器")
@RestController
@RequestMapping("/tool/report")
public class CustomReportController {
    @Autowired
    private CustomReportService customReportService;

    @Operation(summary = "查询自定义报列表", description = "查询自定义报列表")
    @PostMapping("/pageList")
    public Result<IPage<CustomReport>> pageList(@Parameter(description = "查询参数") @RequestBody CustomReportParamVO customReportParamVO) {
        Page<CustomReport> page = new Page<>(customReportParamVO.getPage(), customReportParamVO.getPageSize());
        CustomReport customReport = new CustomReport();
        BeanUtils.copyProperties(customReportParamVO, customReport);
        QueryWrapper<CustomReport> wrapper = QueryWrapperUtil.build(customReport);
        IPage<CustomReport> list = customReportService.page(page, wrapper);
        return Result.success(list);
    }

    @Operation(summary = "获取自定义报详细信息", description = "获取自定义报详细信息")
    @GetMapping(value = "/{id}")
    public Result getInfo(@Parameter(description = "自定义报ID") @PathVariable("id") Long id) {
        CustomReportVO customReportVO = customReportService.getReportInfo(id);
        return Result.success(customReportVO);
    }

    @Operation(summary = "新增自定义报", description = "新增自定义报")
    @PostMapping
    public Result save(@Parameter(description = "自定义报") @RequestBody CustomReportVO customReportVO) {
        return Result.success(customReportService.saveReport(customReportVO));
    }

    @Operation(summary = "删除自定义报", description = "删除自定义报")
    @DeleteMapping
    public Result remove(@Parameter(description = "删除ID") @RequestParam("ids") Long[] ids) {
        return Result.success(customReportService.removeByIds(Arrays.asList(ids)));
    }

    @Operation(summary = "解析sql脚本", description = "解析sql脚本")
    @PostMapping("/parse/sql")
    public Result<List<CustomReportField>> parseSql(@RequestBody CustomReportVO customReportVO) {
        return Result.success(customReportService.parseSql(customReportVO));
    }

    @Operation(summary = "获取报表数据", description = "获取报表数据")
    @PostMapping("/data")
    public Result<IPage<Map<String, Object>>> data(@RequestBody CustomReportParamVO customReportParamVO) {
        Page<Map<String, Object>> page = new Page<>(customReportParamVO.getPage(), customReportParamVO.getPageSize());
        IPage<Map<String, Object>> pageList = customReportService.getReportData(page, customReportParamVO);
        return Result.success(pageList);
    }

    @Operation(summary = "获取报表字段信息", description = "获取报表字段信息")
    @GetMapping("/fields/{code}")
    public Result<List<CustomReportField>> reportFields(@PathVariable("code") String code) {
        return Result.success(customReportService.getFieldByCode(code));
    }
}